{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"http://landlab.github.io\"><img style=\"float: left\" src=\"../../landlab_header.png\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using the Landlab NetworkSedimentTransporter component starting with a shapefile river network\n",
    "\n",
    "<hr>\n",
    "<small>For more Landlab tutorials, click here: <a href=\"https://landlab.readthedocs.io/en/latest/user_guide/tutorials.html\">https://landlab.readthedocs.io/en/latest/user_guide/tutorials.html</a></small>\n",
    "<hr>\n",
    "\n",
    "This tutorial illustrates how to model the transport of coarse sediment through a river network using the NetworkSedimentTransporter Landlab component. For an equivalent tutorial demonstrating initialization of the NetworkSedimentTransporter with a *synthetic network model grid*, [here](../network_sediment_transporter/network_sediment_transporter.ipynb).\n",
    "\n",
    "\n",
    "In this example we will: \n",
    "- load a river network shapefile to create a Landlab grid to represent a river network\n",
    "- create sediment \"parcels\" that will transport through the river network, represented as items in a Landlab DataRecord\n",
    "- run the component\n",
    "- plot the results of the model run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the necessary libraries, plus a bit of magic so that we can plot within this notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import os\n",
    "import pathlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from landlab.components import FlowDirectorSteepest, NetworkSedimentTransporter\n",
    "from landlab.data_record import DataRecord\n",
    "from landlab.grid.network import NetworkModelGrid\n",
    "from landlab.plot import graph\n",
    "from landlab.io import read_shapefile\n",
    "from landlab import ExampleData\n",
    "\n",
    "from landlab.plot import plot_network_and_parcels\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Load a shapefile that represents the river network\n",
    "\n",
    "First, we need to create a Landlab NetworkModelGrid to represent the river network. Each link on the grid represents a reach of river. Each node represents a break between reaches. All tributary junctions must be associated with grid nodes. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datadir = ExampleData(\"io/shapefile\", case=\"methow\").base\n",
    "\n",
    "shp_file = datadir / \"MethowSubBasin.shp\"\n",
    "points_shapefile = datadir / \"MethowSubBasin_Nodes_4.shp\"\n",
    "\n",
    "grid = read_shapefile(\n",
    "    shp_file,\n",
    "    points_shapefile=points_shapefile,\n",
    "    node_fields=[\"usarea_km2\", \"Elev_m\"],\n",
    "    link_fields=[\"usarea_km2\", \"Length_m\"],\n",
    "    link_field_conversion={\"usarea_km2\": \"drainage_area\", \"Slope\":\"channel_slope\", \"Length_m\":\"reach_length\"},\n",
    "    node_field_conversion={\n",
    "        \"usarea_km2\": \"drainage_area\",\n",
    "        \"Elev_m\": \"topographic__elevation\",\n",
    "    },\n",
    "    threshold=0.01,\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alright, let's see what fields we read in with this shapefile:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.at_link.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.at_node.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! Looks like we have length (reach length), upstream drainage area (drainage area), x and y verticies of each link/reach (x and y of polyline), and bed elevation (topographic elevation).\n",
    "\n",
    "Note that \"reach_length\" is defined by the user, rather than calculated as the minimum distance between nodes. This accounts for channel sinuosity. In this case, \"reach_length\" could be equivalently calculated as the cumulative distance between verticies defined by x and y of polyline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph.plot_graph(grid, at=\"node,link\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.number_of_links"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.number_of_nodes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our network consists of 29 links between 30 nodes. In the plot above, X and Y represent the plan-view coordinates of the node locations. \n",
    "\n",
    "Next, we need to populate the grid with the relevant topographic and hydrologic information: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.at_node[\"bedrock__elevation\"] = grid.at_node[\"topographic__elevation\"].copy()\n",
    "\n",
    "grid.at_link[\"channel_width\"] = 1 * np.ones(grid.number_of_links) # m\n",
    "\n",
    "grid.at_link[\"flow_depth\"] = 0.5 * np.ones(grid.number_of_links) # m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We must distinguish between topographic elevation (the top surface of the bed sediment) and bedrock elevation (the surface of the river in the absence of modeled sediment)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create sediment 'parcels' in a DataRecord\n",
    "\n",
    "We represent sediment in the network as discrete parcels (or packages) of grains of uniform size and characteristics. Each parcel is tracked through the network grid according to sediment transport and stratigraphic constraints. \n",
    "\n",
    "Parcels are tracked using the Landlab [DataRecord](../data_record/DataRecord_tutorial.ipynb).\n",
    "\n",
    "First, let's create arrays with all of the essential sediment parcel variables: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# element_id is the link on which the parcel begins. \n",
    "element_id = np.repeat(np.arange(grid.number_of_links), 50)\n",
    "element_id = np.expand_dims(element_id, axis=1)\n",
    "\n",
    "volume = 1*np.ones(np.shape(element_id))  # (m3)\n",
    "active_layer = np.ones(np.shape(element_id)) # 1= active, 0 = inactive\n",
    "density = 2650 * np.ones(np.size(element_id))  # (kg/m3)\n",
    "abrasion_rate = 0 * np.ones(np.size(element_id)) # (mass loss /m)\n",
    "\n",
    "# Lognormal GSD\n",
    "medianD = 0.15 # m\n",
    "mu = np.log(medianD)\n",
    "sigma = np.log(2) #assume that D84 = sigma*D50\n",
    "np.random.seed(0)\n",
    "D = np.random.lognormal(\n",
    "    mu,\n",
    "    sigma,\n",
    "    np.shape(element_id)\n",
    ")  # (m) the diameter of grains in each parcel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to track sediment motion, we classify parcels as either active (representing mobile surface sediment) or inactive (immobile subsurface) during each timestep. The active parcels are the most recent parcels to arrive in the link. During a timestep, active parcels are transported downstream (increasing their `location_in_link`, which is a normalized value ranging from 0 to 1) according to a sediment transport formula. \n",
    "\n",
    "We begin by assigning each parcel an arbitrary (and small) arrival time and location in the link. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_arrival_in_link = np.random.rand(np.size(element_id), 1) \n",
    "location_in_link = np.random.rand(np.size(element_id), 1) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to the required parcel attributes listed above, you can designate optional parcel characteristics, depending on your needs. For example: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lithology = [\"quartzite\"] * np.size(element_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now collect the arrays into a dictionary of variables, some of which will be tracked through time (`[\"item_id\", \"time\"]`), and others of which will remain constant through time :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "variables = {\n",
    "    \"abrasion_rate\": ([\"item_id\"], abrasion_rate),\n",
    "    \"density\": ([\"item_id\"], density),\n",
    "    \"lithology\": ([\"item_id\"], lithology),\n",
    "    \"time_arrival_in_link\": ([\"item_id\", \"time\"], time_arrival_in_link),\n",
    "    \"active_layer\": ([\"item_id\", \"time\"], active_layer),\n",
    "    \"location_in_link\": ([\"item_id\", \"time\"], location_in_link),\n",
    "    \"D\": ([\"item_id\", \"time\"], D),\n",
    "    \"volume\": ([\"item_id\", \"time\"], volume)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With all of the required attributes collected, we can create the parcels DataRecord. Often, parcels will eventually transport off of the downstream-most link. To track these parcels, we have designated a \"`dummy_element`\" here, which has index value `-2`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "items = {\"grid_element\": \"link\", \"element_id\": element_id}\n",
    "\n",
    "parcels = DataRecord(\n",
    "    grid,\n",
    "    items=items,\n",
    "    time=[0.0],\n",
    "    data_vars=variables,\n",
    "    dummy_elements={\"link\": [NetworkSedimentTransporter.OUT_OF_NETWORK]},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Run the NetworkSedimentTransporter\n",
    "\n",
    "With the parcels and grid set up, we can move on to setting up the model. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "timesteps = 10 # total number of timesteps\n",
    "dt = 60 * 60 * 24 *2 # length of timestep (seconds) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before running the NST, we need to determine flow direction on the grid (upstream and downstream for each link). To do so, we initalize and run a Landlab flow director component: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fd = FlowDirectorSteepest(grid, \"topographic__elevation\")\n",
    "fd.run_one_step()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we initialize the network sediment transporter: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nst = NetworkSedimentTransporter(    \n",
    "    grid,\n",
    "    parcels,\n",
    "    fd,\n",
    "    bed_porosity=0.3,\n",
    "    g=9.81,\n",
    "    fluid_density=1000,\n",
    "    transport_method=\"WilcockCrowe\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we are ready to run the model forward in time: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(0, (timesteps * dt), dt):\n",
    "    nst.run_one_step(dt)\n",
    "    \n",
    "    print(\"Model time: \", t/(60*60*24), \"days passed\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Plot the model results\n",
    "\n",
    "There are landlab plotting tools specific to the `NetworkSedimentTransporter`. In particular, `plot_network_and_parcels` creates a plan-view map of the network and parcels (represented as dots along the network). We can color both the parcels and the links by attributes.  \n",
    "\n",
    "Here, we demonstrate one example use of `plot_network_and_parcels`. For a thorough tutorial on the plotting tools, see [this notebook](../network_sediment_transporter/network_plotting_examples.ipynb).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can color links by values that we calculate. For example, if we are curious about the fate of sediment that started out on link 27, we might want to plot the total volume of sediment that originated on link 27 during a later timestep:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "timestep_of_interest = 6\n",
    "originating_link = 27\n",
    "\n",
    "#filter the parcels to calculate total volumes of only the parcels that originated in the chosen link\n",
    "parcelfilter = np.zeros_like(\n",
    "    parcels.dataset.element_id, dtype=bool\n",
    ")\n",
    "parcelfilter[:, timestep_of_interest] = (parcels.dataset.element_id[:,0] == originating_link)\n",
    "\n",
    "vol_orig_link = parcels.calc_aggregate_value(\n",
    "            np.sum,\n",
    "            \"volume\",\n",
    "            at=\"link\",\n",
    "            filter_array=parcelfilter,\n",
    "            fill_value=0.0\n",
    "        )\n",
    "\n",
    "fig = plot_network_and_parcels(\n",
    "    grid, parcels,\n",
    "    link_attribute=vol_orig_link, \n",
    "    link_attribute_title = \"Vol of sed originating on link x\",\n",
    "    network_linewidth = 5,\n",
    "    parcel_alpha = 0\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Non-network plotting\n",
    "The results of the NST can be visualized by directly accessing information about the grid, the parcels, and by accessing variables stored after the run of NST. \n",
    "\n",
    "As a simple example, we can plot the total volume of parcels on the grid through time. As parcels exit the grid, the total volume decreases.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "parcel_vol_on_grid = parcels.dataset[\"volume\"].values\n",
    "parcel_vol_on_grid[parcels.dataset[\"element_id\"].values==-2]=0\n",
    "\n",
    "#plt.figure(figsize=(8,6))\n",
    "plt.plot(np.asarray(parcels.time_coordinates)/(60*60*24), \n",
    "         np.sum(parcel_vol_on_grid, axis=0),\n",
    "         '-',\n",
    "         linewidth=3, \n",
    "         alpha=0.5\n",
    "        )\n",
    "\n",
    "plt.ylabel('Total volume of parcels on grid $[m^3]$')\n",
    "plt.xlabel('Time [days]')\n",
    "plt.show() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also plot individual parcel characteristics. The plot below shows the total transport distance of each parcel through the whole model run as a function of the parcel's grain size (during the final timestep). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.loglog(parcels.dataset.D[:,-1],\n",
    "         nst._distance_traveled_cumulative,\n",
    "         '.'\n",
    "        )\n",
    "plt.xlabel('Parcel grain size (m)')\n",
    "plt.ylabel('Cumulative parcel travel distance (m)')\n",
    "\n",
    "# Note: some of the smallest grain travel distances can exceed the length of the \n",
    "# grid by \"overshooting\" during a single timestep of high transport rate\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plot below is an example of accessing variables associated with the grid (`grid.at_link.X`, or `grid.at_node.X`), as well as a variable associated with this instance of NetworkModelGrid (`nmg.X`):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(grid.at_link[\"channel_slope\"],\n",
    "         nst.d_mean_active, \n",
    "         '.')\n",
    "plt.xlabel('Channel slope (m/m)')\n",
    "plt.ylabel('Mean grain size of active layer (m)')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
